package com.waa.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.waa.gulimall.product.entity.AttrEntity;
import com.waa.gulimall.product.service.AttrAttrgroupRelationService;
import com.waa.gulimall.product.service.AttrService;
import com.waa.gulimall.product.service.CategoryService;
import com.waa.gulimall.product.vo.AttrGroupRelationVo;
import com.waa.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.waa.gulimall.product.entity.AttrGroupEntity;
import com.waa.gulimall.product.service.AttrGroupService;
import com.waa.gulimall.common.utils.PageUtils;
import com.waa.gulimall.common.utils.R;


/**
 * 属性分组
 *
 * @author waa
 * @email 2460335742@qq.com
 * @date 2022-02-22 09:57:25
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * /product/attr/sale/list/{catelogId}
     * 列表
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entity = attrService.getAttrRelation(attrgroupId);
        return R.ok().put("data", entity);
    }

    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long catelogId) {
        //  PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        final Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

    /*
     * 批量删除属性分组的关联关系
     * */
    //product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
        attrGroupService.deleteRelation(vos);
        return R.ok();
    }

    @GetMapping("{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId, @RequestParam Map<String, Object> params) {
        final PageUtils pageUtils = attrService.getNoRelationAttr(params, attrgroupId);
        return R.ok().put("page", pageUtils);
    }

    //添加属性与分组关联关系
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody AttrGroupRelationVo [] arv){
        relationService.saveBatch(arv);
        return R.ok();
    }
    ///product/attrgroup/{catelogId}/withattr
    //获取分类下所有分组&关联属性
    @GetMapping("/{catelogId}/withattr")
    public  R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
           List<AttrGroupWithAttrsVo> list = attrGroupService.getAttrGroupWithAttrs(catelogId);
           return R.ok().put("data",list);
    }

}
